<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        {{ message }}
    </div>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                message: 'Hello Vue!'
            }
        })

    </script>

    <div id="app-2">
        <span v-bind:title='message'>
            鼠标悬停几秒钟查看此处动态绑定的提示信息！
        </span>
    </div>
    <script>
        var app2 = new Vue({
            el: '#app-2',
            data: {
                message: '页面加载于 ' + new Date().toLocaleString()
            }
        })

        setTimeout(function () {
            app2.message = '新消息'
        }, 3000)
    </script>

    <div id="app-3">
        <p v-if="seen">现在你看到我了</p>
    </div>
    <script>
        var app3 = new Vue({
            el: '#app-3',
            data: {
                seen: false
            }
        })

        setTimeout(function () {
            app3.seen = true
        }, 3000)
    </script>

    <div id="app-4">
        <ol>
            <li v-for="todo in todos">
                {{ todo.text }}
            </li>
        </ol>
    </div>
    <script>
        var app4 = new Vue({
            el: '#app-4',
            data: {
                todos: [
                    { text: '学习 JavaScript' },
                    { text: '学习 Vue' },
                    { text: '整个牛项目' }
                ]
            }
        })
    </script>

    <div id="app-5">
        <p>{{ message }}</p>
        <button v-on:click="reverseMessage">反转消息</button>
    </div>
    <script>
        var app5 = new Vue({
            el: '#app-5',
            data: {
                message: 'Hello Vue.js!'
            },
            methods: {
                reverseMessage: function () {
                    this.message = this.message.split('').reverse().join('')
                }
            }
        })
    </script>

    <div id="app-6">
        <p>{{ message }}</p>
        <input v-model="message">
    </div>
    <script>
        var app6 = new Vue({
            el: '#app-6',
            data: {
                message: 'Hello Vue!'
            }
        })
    </script>

    <div id="app-7">
        <ol>
            <todo-item v-for="item in groceryList" v-bind:todo="item" v-bind:key="item.id"></todo-item>
        </ol>
    </div>
    <script>
        Vue.component('todo-item', {
            props: ['todo'],
            template: '<li>{{ todo.text }}</li>'
        })

        var app7 = new Vue({
            el: '#app-7',
            data: {
                groceryList: [
                    { id: 0, text: '蔬菜' },
                    { id: 1, text: '奶酪' },
                    { id: 2, text: '随便其它什么人吃的东西' }
                ]
            }
        })
    </script>

    <div id="todo-list-example">
        <form v-on:submit.prevent="addNewTodo">
            <label for="new-todo">Add a todo</label>
            <input v-model="newTodoText" id="new-todo" placeholder="E.g. Feed the cat">
            <button>Add</button>
        </form>
        <ul>
            <todo-item v-for="(todo, index) in todos" v-bind:key="todo.id" v-bind:title="todo.title"
                v-on:remove="todos.splice(index, 1)"></todo-item>
        </ul>
    </div>
    <script>
        Vue.component('todo-item', {
            template: '\
                <li>\
                {{ title }}\
                <button v-on:click="$emit(\'remove\')">Remove</button>\
                </li>\
            ',
            props: ['title']
        })

        new Vue({
            el: '#todo-list-example',
            data: {
                newTodoText: '',
                todos: [
                    {
                        id: 1,
                        title: 'Do the dishes',
                    },
                    {
                        id: 2,
                        title: 'Take out the trash',
                    },
                    {
                        id: 3,
                        title: 'Mow the lawn'
                    }
                ],
                nextTodoId: 4
            },
            methods: {
                addNewTodo: function () {
                    this.todos.push({
                        id: this.nextTodoId++,
                        title: this.newTodoText
                    })
                    this.newTodoText = ''
                }
            }
        })
    </script>

    <div id="id_selected">
        <select v-model="selected">
            <option v-for="option in options" v-bind:value="option.value">
                {{ option.text }}
            </option>
        </select>
        <span>Selected: {{ selected }}</span>
    </div>

    <script>
        new Vue({
            el: '#id_selected',
            data: {
                selected: 'A',
                options: [
                    { text: 'One', value: 'A' },
                    { text: 'Two', value: 'B' },
                    { text: 'Three', value: 'C' }
                ]
            }
        })
    </script>

    <div id="components-demo">
        <button-counter></button-counter>
        <button-counter></button-counter>
        <button-counter></button-counter>
    </div>
    <script>
        // 定义一个名为 button-counter 的新组件
        Vue.component('button-counter', {
            data: function () {
                return {
                    count: 0
                }
            },
            template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>'
        })
        new Vue({ el: '#components-demo' })
    </script>

    <div id="blog-posts-events-demo">
        <blog-post v-for="post in posts" :key="post.id" :post="post"></blog-post>
    </div>

    <script>
        Vue.component('blog-post', {
            props: ['post'],
            template: `
                <div class="blog-post" :style='{fontSize: post.postFontSize + "em"}'>
                    <button @click="clicked"> {{ post.title }} button </button>
                    <h3>{{ post.title }}</h3>
                    <div v-html="post.content"></div>
                </div>
            `,
            methods: {
                clicked: function(event) {
                    // this.$emit('enlarge-text')
                    this.post.postFontSize += 0.1
                    console.log('this.post.postFontSize: ', this.post.postFontSize)
                }
            }
        })
        new Vue({
            el: '#blog-posts-events-demo',
            data: {
                posts:
                    [{
                        id: 1,
                        title: "I'm a title 1",
                        content: "I'm content 1",
                        postFontSize: 1
                    }, {
                        id: 2,
                        title: "I'm a title 2",
                        content: "I'm content 2",
                        postFontSize: 1
                    }],
            }
        })
    </script>

</body>

</html>